<?php



namespace App\Http\Controllers\Admin;

use App\Helpers\Pinyin;
use App\Models\Treegame;
/**
 * Description of Treegame
 *
 * @author yonguo
 */
class TreegameController extends BaseController{
   
    public function index()
    {
        if ($this->isOnSubmit()) {
            $catloglist = $this->request->input('catloglist');
            if ($catloglist) {
                $pinyin = new Pinyin();
                foreach ($catloglist as $catid=>$catlog) {
                    if ($catlog['name']) {
                        if (!$catlog['identifier']) {
                            $catlog['identifier'] = $pinyin->getPinyin($catlog['name']);
                        }
                         $catlog['nonpoint'] = intval($catlog['nonpoint']);
                        $catlog['enable'] = intval($catlog['enable']);
                        $catlog['displayorder'] = intval($catlog['displayorder']);
                        Treegame::where('catid', $catid)->update($catlog);
                    }
                }
                Treegame::updateCache();
            }
            return $this->showSuccess('sysmessage.info save success');
        }else {

            $this->assign(['catloglist'=>$this->getCatlogs()]);

            return $this->view('admin.treegame.treegame');
        }
        
        
    }
    
    public function showSuccess($message)
    {
        return $this->messager()->message(trans($message))->render();
        
    }
    
    public function newtreecat(){

        $catid = $this->request->input('catid');
        if ($this->isOnSubmit()){
            $catlog = $this->request->post('catlog');
            //dd($catlog);
           // return;
            $pinyin = new Pinyin();
            $catlog['identifier'] = $pinyin->getPinyin($catlog['name']);
            if ($catid) {
                Treegame::where('catid', $catid)->update($catlog);
            }else {
                Treegame::insert($catlog);
            }
            Treegame::updateCache();
            return $this->showSuccess(trans('ui.save_succeed'));
        }else {
            $this->assign([
                'catid'=>$catid,
                'catlog'=>[
                    'name'=>'',
                    'nonpoint'=>'',
                    'fid'=>0,
                    'available'=>1,
                    'template_index'=>'',
                    'template_list'=>'',
                    'template_detail'=>'',
                    'keywords'=>'',
                    'description'=>''
                ],
                'catloglist'=>$this->getCatlogs()
            ]);

            if ($catid) {
                $catlog = Treegame::find($catid);
                if ($catlog) $this->assign(['catlog'=>$catlog]);
            }

            return $this->view('admin.treegame.newtreecat');
        }
    }
    
    /**
     * 删除树种
     * @return type
     */
    public function delete(){
        $catid = $this->request->input('catid');
       
           Treegame::where('catid', $catid)->delete();
           
            Treegame::updateCache();
            return $this->messager()->message(trans('sysmessage.info delete success'))
                ->links([
                    [trans('sysmessage.back_list'), url('admin/treegame')]
                ])->render();
    }

    
    /**
     * @return array|mixed
     */
    private function getCatlogs()
    {
        $catloglist = [];
        Treegame::with('childs')->orderBy('displayorder')->get()->map(function ($catlog) use (&$catloglist){
            $catloglist[$catlog->fid][$catlog->catid] = $catlog;
        });

        if ($catloglist) {
            return $catloglist[0];
        } else {
            return [];
        }
    }
}
